﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureBasicBeam.h"
#include "BeamStartAndEndOffsetParameterUpdater.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "GbmpBuiltInParameterStaticVauleListItems.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "GmStructureDefaultElementParametersCustomizer.h"
#include "ICalculator.h"
#include "ICalculatorCollection.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElement.h"
#include "IElementParameters.h"
#include "IElementParentReporter.h"
#include "IElementPosition.h"
#include "IElementStatus.h"
#include "IExternalDataComponent.h"
#include "IFamily.h"
#include "IInstance.h"
#include "IInstanceType.h"
#include "ILevel.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IPositionCurve2d.h"
#include "IPositionSingleAssociatedPlane.h"
#include "IPositionSingleAssociatedPlaneWithTwoOffsets.h"
#include "IRegenerator.h"
#include "IRegeneratorDataIdCreator.h"
#include "LevelUtils.h"
#include "StructureBeamEndTypeBehavior.h"
#include "StructureBeamInput.h"
#include "StructureBeamPositionPoints.h"
#include "StructureInstanceLevelUtils.h"
#include "StructureInstanceSectionShapeType.h"
#include "StructureInstanceUtils.h"
#include "StructureBeamElementParametersCustomizer.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureBasicBeam, StartEndFaceSectionType)
IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureBasicBeam, Angle)
IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureBasicBeam, BottomElevation)
IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureBasicBeam, StartTopElevation)
IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureBasicBeam, EndTopElevation)

DEFINE_STRUCTURE_INSTANCE_BASE_DATA_METHODS(StructureBasicBeam, BaseData);

DBOBJECT_DATA_DEFINE(StructureBasicBeam)
{
    SetStartEndFaceSectionType__(InstanceEndType::Vertical);
    SetAngle__(0.0);
    SetBottomElevation__(0);
    SetStartTopElevation__(0);
    SetEndTopElevation__(0);
}

StructureBasicBeam* StructureBasicBeam::Create(const StructureBeamInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    IInstance* pInstance = nullptr;
    if (pInput->PositionType == ElementInputPositionType::Line)
    {
        pInstance = StructureBasicBeam::CreateStraightBeam(pInput);
    }
    else if (pInput->PositionType == ElementInputPositionType::ArcBy3Points)
    {
        pInstance = StructureBasicBeam::CreateArcBy3PtsBeam(pInput);
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    StructureBasicBeam* pBasicBeam = StructureBasicBeam::Get(pInstance);
    return pBasicBeam;
}

IInstance* StructureBasicBeam::CreateStraightBeam(const StructureBeamInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pInput->Document;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IInstanceType* pInstanceType = quick_cast<IInstanceType>(pDoc->GetElement(pInput->InstanceTypeId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstanceType, L"pInstanceType为空",L"GDMPLab",L"2024-03-30");

    IInstance* pInstance = pInstanceType->CreateInstance(L"梁实例", pInput->CanBeShared, pInput->CreationOption, pInput->IsCreateGeometryRelationshipComponent);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    //设置instance的Unidentity
    if (pInput->categoryUid.IsValid()) 
    {
        pInstance->GetBasicInformation()->SetCategoryUid(pInput->categoryUid);
    }
    
    StructureBasicBeam* pBeam = StructureBasicBeam::RegisterExternalData(pInstance, pInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pBeam, L"pBeam为空",L"GDMPLab",L"2024-03-30");

    ElementId associatedPlaneId = pInput->AssociatedPlaneId;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(associatedPlaneId.IsValid(), L"associatedPlaneId无效",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pInput->StoreyId.IsValid())
    {
        pStoreyLevel = LevelUtils::GetLevelById(pDoc, pInput->StoreyId);
    }
    else
    {
        pStoreyLevel = StructureInstanceLevelUtils::GetBeamStoreyLevelFromPositioningLevel(pDoc, associatedPlaneId);
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");

    bool bOk = StructureInstanceLevelUtils::CreateInstanceStoreyLevel(pInstance, pStoreyLevel);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"SetBeamStoreyLevel失败",L"GDMPLab",L"2024-03-30");

    bOk = IElementPosition::CreateCurve2dOnTopAssociatedPlaneWithTwoOffsetsPositionBehavior(pInstance, associatedPlaneId, pInput->PositionPoints);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"PositionBehavior创建失败",L"GDMPLab",L"2024-03-30");

    IElementPosition* pPositionBehavior = pInstance->GetElementPosition();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionBehavior, L"pPositionBehavior为空",L"GDMPLab",L"2024-03-30");

    IPositionSingleAssociatedPlaneWithTwoOffsets* pPositionAssociatedPlaneWithTwoOffsets =
        quick_cast<IPositionSingleAssociatedPlaneWithTwoOffsets>(pPositionBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionAssociatedPlaneWithTwoOffsets, L"pPositionAssociatedPlaneWithTwoOffsets为空",L"GDMPLab",L"2024-03-30");
    pPositionAssociatedPlaneWithTwoOffsets->SetIsAllowAssociatedPlaneOffsetParameter(true);
    pPositionAssociatedPlaneWithTwoOffsets->SetAssociatedPlaneOffsetParameterUid(PARAMETER_UID(TopOffsetBuiltInParameter));
    pPositionAssociatedPlaneWithTwoOffsets->SetBasePlaneOffset(pInput->AssociatedPlaneOffset);
    pPositionAssociatedPlaneWithTwoOffsets->SetStartOffset(pInput->StartOffset);
    pPositionAssociatedPlaneWithTwoOffsets->SetEndOffset(pInput->EndOffset);

    OwnerPtr<IInstanceFamilyParametersUpdater> opParametersUpdater = NEW_AS_OWNER_PTR(BeamStartAndEndOffsetParameterUpdater);
    pPositionBehavior->SetParametersUpdater(TransferOwnership(opParametersUpdater));

    pInstance->SetPositionPoints(NEW_AS_OWNER_PTR(StructureBeamPositionPoints, pInstance));

    StructureInstanceUtils::UpdateBeamCoordinateSystemParameter(pInstance);

    OwnerPtr<IParameter> paramZPositioningType = pInstance->GetElementParameters()->
        GetParameterByUid(PARAMETER_UID(BeamZPositioningTypeBuiltInParameter));
    if (paramZPositioningType)
    {
        paramZPositioningType->SetValueAsInt((int)pInput->ZPositioningType);
        pInstance->GetElementParameters()->SetParameter(paramZPositioningType.get());
    }

    OwnerPtr<StructureBeamEndTypeBehavior> opEndTypeBehavior = NEW_AS_OWNER_PTR(StructureBeamEndTypeBehavior, pInstance);
    pInstance->AddGraphicsElementShapeCustomizableBehavior(TransferOwnership(opEndTypeBehavior), GuidUtils::GetInvalid());

    if (pInstance->GetElementParameters())
    {
        OwnerPtr<IElementParametersCustomizer> opNewCustomizer = 
            NEW_AS_OWNER_PTR(StructureBeamElementParametersCustomizer, pInstance);
        GmStructureDefaultElementParametersCustomizer::SetElementParametersCustomizer(
            pInstance, TransferOwnership(opNewCustomizer));
    }

    IElementStatus* pStatus = pInstance->GetStatus();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStatus, L"无法获取状态集",L"GDMPLab",L"2024-03-30");
    pStatus->SetIsVisible(pInput->Visible);

    bOk = pDoc->GetRegenerator()->RegenerateElement(pInstance->GetElementId());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"RegenerateElement失败",L"GDMPLab",L"2024-03-30");

    return pInstance;
}

IInstance* StructureBasicBeam::CreateArcBy3PtsBeam(const StructureBeamInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pInput->Document;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IInstanceType* pInstanceType = quick_cast<IInstanceType>(pDoc->GetElement(pInput->InstanceTypeId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstanceType, L"pInstanceType为空",L"GDMPLab",L"2024-03-30");

    IInstance* pInstance = pInstanceType->CreateInstance(L"梁实例", pInput->CanBeShared, pInput->CreationOption, pInput->IsCreateGeometryRelationshipComponent);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    
    //设置instance的Unidentity
    if (pInput->categoryUid.IsValid())
    {
        pInstance->GetBasicInformation()->SetCategoryUid(pInput->categoryUid);
    }

    StructureBasicBeam* pBeam = StructureBasicBeam::RegisterExternalData(pInstance, pInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pBeam, L"pBeam为空",L"GDMPLab",L"2024-03-30");

    ElementId associatedPlaneId = pInput->AssociatedPlaneId;
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(associatedPlaneId.IsValid(), L"associatedPlaneId无效",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pInput->StoreyId.IsValid())
    {
        pStoreyLevel = LevelUtils::GetLevelById(pDoc, pInput->StoreyId);
    }
    else
    {
        pStoreyLevel = StructureInstanceLevelUtils::GetBeamStoreyLevelFromPositioningLevel(pDoc, associatedPlaneId);
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    bool bOk = StructureInstanceLevelUtils::CreateInstanceStoreyLevel(pInstance, pStoreyLevel);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"SetBeamStoreyLevel失败",L"GDMPLab",L"2024-03-30");

    bOk = IElementPosition::CreateCurve2dOnTopAssociatedPlanePositionBehavior(pInstance, associatedPlaneId, pInput->PositionPoints);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"PositionBehavior创建失败",L"GDMPLab",L"2024-03-30");

    IElementPosition* pPositionBehavior = pInstance->GetElementPosition();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionBehavior, L"pPositionBehavior为空",L"GDMPLab",L"2024-03-30");

    IPositionSingleAssociatedPlane* pPositionSingleAssociatedPlane =
        quick_cast<IPositionSingleAssociatedPlane>(pPositionBehavior->GetPositionAssociatedPlane());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositionSingleAssociatedPlane, L"pPositionSingleAssociatedPlane为空",L"GDMPLab",L"2024-03-30");
    pPositionSingleAssociatedPlane->SetOffset(pInput->AssociatedPlaneOffset);

    pInstance->SetPositionPoints(NEW_AS_OWNER_PTR(StructureBeamPositionPoints, pInstance));

    const IPositionCurve2d* pPosCurve2d = dynamic_cast<const IPositionCurve2d*>(pPositionBehavior->GetPositionGeometry());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPosCurve2d, L"pPosCurve2d为空",L"GDMPLab",L"2024-03-30");
    bOk = pInstance->SetCoordinateSystemsByCurve(pPosCurve2d->GetBaseCurve());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"SetCoordinateSystemsByCurve失败",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IParameter> paramZPositioningType = pInstance->GetElementParameters()->
        GetParameterByUid(PARAMETER_UID(BeamZPositioningTypeBuiltInParameter));
    if (paramZPositioningType)
    {
        paramZPositioningType->SetValueAsInt((int)pInput->ZPositioningType);
        pInstance->GetElementParameters()->SetParameter(paramZPositioningType.get());
    }

    if (pInstance->GetElementParameters())
    {
        OwnerPtr<IElementParametersCustomizer> opNewCustomizer = 
            NEW_AS_OWNER_PTR(StructureBeamElementParametersCustomizer, pInstance);
        GmStructureDefaultElementParametersCustomizer::SetElementParametersCustomizer(
            pInstance, TransferOwnership(opNewCustomizer));
    }

    IElementStatus* pStatus = pInstance->GetStatus();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStatus, L"无法获取状态集",L"GDMPLab",L"2024-03-30");
    pStatus->SetIsVisible(pInput->Visible);

    bOk = pDoc->GetRegenerator()->RegenerateElement(pInstance->GetElementId());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"RegenerateElement失败",L"GDMPLab",L"2024-03-30");

    return pInstance;
}

StructureBasicBeam* StructureBasicBeam::Get(IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        IExternalData* pData = pExternalDataComponent->FindExternalData(StructureBasicBeam::GetStaticClassSchema()->GetName());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");

        return quick_cast<StructureBasicBeam>(pData);
    }
    return nullptr;
}

const StructureBasicBeam* StructureBasicBeam::Get(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (const IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        const IExternalData* pData = pExternalDataComponent->FindExternalData(StructureBasicBeam::GetStaticClassSchema()->GetName());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");

        return quick_cast<const StructureBasicBeam>(pData);
    }
    return nullptr;
}

StructureBasicBeam* StructureBasicBeam::RegisterExternalData(IInstance* pInstance, const StructureBeamInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"pInput为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureBasicBeam> opBasicBeam = NEW_AS_OWNER_PTR(StructureBasicBeam);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBasicBeam, L"opBasicBeam为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureBeam> opBaseData = StructureBeam::Create(pInstance, pInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opBaseData, L"opBaseData为空",L"GDMPLab",L"2024-03-30");
    opBasicBeam->SetBaseData__(TransferOwnership(opBaseData));

    opBasicBeam->SetStartEndFaceSectionType(pInput->StartEndFaceSectionType);

    StructureBasicBeam* pBasicBeam = opBasicBeam.get();
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        pExternalDataComponent->RegisterExternalData(StructureBasicBeam::GetStaticClassSchema()->GetName(), TransferOwnership(opBasicBeam));
    }
    return pBasicBeam;
}

double StructureBasicBeam::GetVolume() const
{
    return GetBaseData__()->GetVolume();
}

bool StructureBasicBeam::SetVolume(double volume)
{
    return GetBaseDataFW__()->SetVolume(volume);
}

double StructureBasicBeam::GetLength() const
{
    return GetBaseData__()->GetLength();
}

bool StructureBasicBeam::SetLength(double length)
{
    return GetBaseDataFW__()->SetLength(length);
}

BeamZPositioningType StructureBasicBeam::GetZPositioningType() const
{
    // TODO:
    return BeamZPositioningType::Top;
}

bool StructureBasicBeam::SetZPositioningType(BeamZPositioningType type)
{
    // TODO:
    return true;
}

StructureInstanceSectionShapeType StructureBasicBeam::GetSectionShapeType() const
{
    const IElement *pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_UNLESS(pElement, StructureInstanceSectionShapeType::SECTION_SHAPE_INVALID, L"pElement为空",L"GDMPLab",L"2024-03-30");
    const IElementBasicInformation *pBasicInfo = pElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_UNLESS(pBasicInfo, StructureInstanceSectionShapeType::SECTION_SHAPE_INVALID, L"pBasicInfo为空",L"GDMPLab",L"2024-03-30");
    const IType *pType = pBasicInfo->GetType();
    DBG_WARN_AND_RETURN_UNLESS(pType, StructureInstanceSectionShapeType::SECTION_SHAPE_INVALID, L"pType为空",L"GDMPLab",L"2024-03-30");
    const IElementParameters* pElementParam = pType->GetElementParameters();
    DBG_WARN_AND_RETURN_UNLESS(pElementParam, StructureInstanceSectionShapeType::SECTION_SHAPE_INVALID, L"pElementParam为空",L"GDMPLab",L"2024-03-30");

    int parameterValue = 0;
    bool hasParameter = pElementParam->GetParameterValueAsInt(PARAMETER_UID(SectionShapeBuiltInParameter), parameterValue);
    DBG_WARN_AND_RETURN_UNLESS(hasParameter,StructureInstanceSectionShapeType::SECTION_SHAPE_INVALID, L"获取截面形状参数失败",L"GDMPLab",L"2024-03-30");

    StructureInstanceSectionShapeType sectionShapeType = (StructureInstanceSectionShapeType)parameterValue;

    return sectionShapeType;
}

void StructureBasicBeam::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    GetBaseDataFW__()->UpdateForWeakParentDeletion(deletedElementIds);
}

void StructureBasicBeam::ReportParents(IElementParentReporter& reporter) const
{
    GetBaseData__()->ReportParents(reporter);
}

DECLARE_CALCULATOR_CREATOR(StructureBasicBeamAngleCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamTopElevationCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamBottomElevationCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamStartTopElevationCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamStartBottomElevationCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamEndTopElevationCalculator)
DECLARE_CALCULATOR_CREATOR(StructureBasicBeamEndBottomElevationCalculator)
void StructureBasicBeam::GetCalculators(ICalculatorCollection * calculators) const
{
    GetBaseData__()->GetCalculators(calculators);

    ADD_CALCULATOR(StructureBasicBeamAngleCalculator, GetDocument(), GetAngleRdId());

    if (StructureInstanceSectionShapeType::SECTION_SHAPE_RECTANGLE == GetSectionShapeType())
    {
        ADD_CALCULATOR(StructureBasicBeamTopElevationCalculator, GetDocument(), GetBaseData__()->GetTopElevationRdId());
        ADD_CALCULATOR(StructureBasicBeamBottomElevationCalculator, GetDocument(), GetBottomElevationRdId());
        ADD_CALCULATOR(StructureBasicBeamStartTopElevationCalculator, GetDocument(), GetStartTopElevationRdId());
        ADD_CALCULATOR(StructureBasicBeamStartBottomElevationCalculator, GetDocument(), GetBaseData__()->GetStartBottomElevationRdId());
        ADD_CALCULATOR(StructureBasicBeamEndTopElevationCalculator, GetDocument(), GetEndTopElevationRdId());
        ADD_CALCULATOR(StructureBasicBeamEndBottomElevationCalculator, GetDocument(), GetBaseData__()->GetEndBottomElevationRdId());
    }
}

void StructureBasicBeam::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    GetBaseData__()->ReportParameterDefinitions(pParamDefIds);
    if (StructureInstanceSectionShapeType::SECTION_SHAPE_RECTANGLE == GetSectionShapeType())
    {
        pParamDefIds->push_back(PARAMETER_ID(TopElevationBuiltInParameter));
        pParamDefIds->push_back(PARAMETER_ID(BottomElevationBuiltInParameter));
        pParamDefIds->push_back(PARAMETER_ID(StartTopElevationBuiltInParameter));
        pParamDefIds->push_back(PARAMETER_ID(EndTopElevationBuiltInParameter));
        pParamDefIds->push_back(PARAMETER_ID(StartBottomElevationBuiltInParameter));
        pParamDefIds->push_back(PARAMETER_ID(EndBottomElevationBuiltInParameter));
    }
    pParamDefIds->push_back(PARAMETER_ID(BeamAngleBuiltInParameter));
}

OwnerPtr<IParameter> StructureBasicBeam::GetNativeParameter(int paramDefId) const
{
    OwnerPtr<IParameter> nativeParam = GetBaseData__()->GetNativeParameter(paramDefId);
    if (IParameter::IsValidParameter(nativeParam))
        return nativeParam;

    OwnerPtr<IParameter> param;
    ParameterAttributes pa = {false, false, true};
    if (paramDefId == PARAMETER_ID(BeamAngleBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(BeamAngleBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(BeamAngleBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetAngle());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(TopElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(TopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(TopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetTopElevation());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(BottomElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(BottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(BottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetBottomElevation());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(StartTopElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(StartTopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(StartTopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetStartTopElevation());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(StartBottomElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(StartBottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(StartBottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetStartBottomElevation());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(EndTopElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(EndTopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(EndTopElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetEndTopElevation());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(EndBottomElevationBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(EndBottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(EndBottomElevationBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetEndBottomElevation());
        return TransferOwnership(param);
    }
    return nullptr;
}

bool StructureBasicBeam::SetNativeParameter(const IParameter *param, std::wstring* errorMsg/* = nullptr*/)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (GetBaseDataFW__()->SetNativeParameter(param, errorMsg))
        return true;

    if (param->GetParameterDefinitionId() == PARAMETER_ID(BottomElevationBuiltInParameter))
    {
        SetBottomElevation(param->GetValueAsDouble());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(StartTopElevationBuiltInParameter))
    {
        SetStartTopElevation(param->GetValueAsDouble());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(EndTopElevationBuiltInParameter))
    {
        SetEndTopElevation(param->GetValueAsDouble());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(BeamAngleBuiltInParameter))
    {
        SetAngle(param->GetValueAsDouble());
        return true;
    }
    return false;
}

bool StructureBasicBeam::IsParameterModifiable(int paramDefId) const
{
    if (!GetBaseData__()->IsParameterModifiable(paramDefId))
        return false;

    if (paramDefId == PARAMETER_ID(BeamAngleBuiltInParameter) ||
        paramDefId == PARAMETER_ID(TopElevationBuiltInParameter) ||
        paramDefId == PARAMETER_ID(BottomElevationBuiltInParameter) ||
        paramDefId == PARAMETER_ID(StartTopElevationBuiltInParameter) ||
        paramDefId == PARAMETER_ID(StartBottomElevationBuiltInParameter) ||
        paramDefId == PARAMETER_ID(EndTopElevationBuiltInParameter) ||
        paramDefId == PARAMETER_ID(EndBottomElevationBuiltInParameter))
    {
        return false;
    }
    return true;
}

void StructureBasicBeam::Report(const ICopyContext & copyContext, ICopyStrategyReporter& reporter) const
{
    GetBaseData__()->Report(copyContext, reporter);
}

bool StructureBasicBeam::SetOwnerElement(IElement* pOwnerElement)
{
    return m_BaseData->SetOwnerElement(pOwnerElement);
}

IElement* StructureBasicBeam::GetOwnerElement()
{
    return m_BaseData->GetOwnerElement();
}

const IElement* StructureBasicBeam::GetOwnerElement() const
{
    return GetBaseData__()->GetOwnerElement();
}

IDocument* StructureBasicBeam::GetDocument() const
{
    return GetBaseData__()->GetDocument();
}

ElementId StructureBasicBeam::GetOwnerElementId() const
{
    return GetBaseData__()->GetOwnerElementId();
}

NdbObject* StructureBasicBeam::GetTopOwnerObject() const
{
    const StructureBeam* pBaseData = GetBaseData__().get();
    if (!pBaseData)
        return nullptr;
    return pBaseData->GetTopOwnerObject();
}
